package com.lhf.fvscore.handler;

import cn.hutool.json.JSONUtil;
import com.lhf.fvscommon.result.Result;
import com.lhf.fvscommon.result.Status;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * <p></p>
 *
 * @author lhf
 * @since 2020/10/22 11:36
 */
@Component
public class AccessDeniedHandler implements org.springframework.security.web.access.AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        response.setCharacterEncoding("utf8");
        PrintWriter writer = response.getWriter();
        writer.println(JSONUtil.toJsonStr(Result.failure(Status.NO_AUTHORITY)));
    }
}
